package com.yzh.pleaSys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzh.pleaSys.mapper.UserLoginMapper;
import com.yzh.pleaSys.pojo.UserLogin;
import com.yzh.pleaSys.service.UserLoginService;
import com.yzh.pleaSys.utils.EncryptUtil;
import org.springframework.stereotype.Service;

@Service
public class UserLoginServiceImpl
        extends ServiceImpl<UserLoginMapper, UserLogin>
        implements UserLoginService {

    /**
     * 根据用户输入的账号和密码进行登录验证
     *
     * @param userLogin
     * @return
     */
    @Override
    public UserLogin findUserLogin(UserLogin userLogin) {
        LambdaQueryWrapper<UserLogin> wrapper = new LambdaQueryWrapper<>();
        return this.getOne(
                wrapper
                        .eq(UserLogin::getUsername, userLogin.getUsername())
                        .eq(UserLogin::getPassword, EncryptUtil.encrypt(userLogin.getPassword()))
        );
    }

    /**
     * 判断用户是否注册过
     *
     * @param userLogin
     * @return
     */
    @Override
    public UserLogin findUser(UserLogin userLogin) {
        LambdaQueryWrapper<UserLogin> wrapper = new LambdaQueryWrapper<>();
        return this.getOne(
                wrapper.eq(UserLogin::getUsername, userLogin.getUsername())
        );
    }

    /**
     * 更改密码
     *
     * @param id
     * @param newPwd
     * @return
     */
    @Override
    public boolean updatePwd(Integer id, String newPwd) {
        System.out.println("newPwd = " + newPwd);
        System.out.println(EncryptUtil.encrypt(newPwd));
        return this.updateById(
                UserLogin.builder()
                        .id(id)
                        .password(EncryptUtil.encrypt(newPwd))
                        .build()
        );
    }

    /**
     * 注册用户
     *
     * @param userLogin
     * @return
     */
    @Override
    public boolean registerUserLogin(UserLogin userLogin) {
        System.out.println("传参 = " + userLogin);
        userLogin.builder()
                .password(EncryptUtil.encrypt(userLogin.getPassword()))
                .build();
        System.out.println("build = " + userLogin);

        userLogin.setPassword(EncryptUtil.encrypt(userLogin.getPassword()));

        System.out.println("set = " + userLogin);
        return this.save(userLogin);
    }


}
